/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using DefaultModuleResolver = System.Qizx.Api.Util.DefaultModuleResolver;
using Check = System.Qizx.Util.Basic.Check;
using DocumentPool = System.Qizx.Xdm.DocumentPool;
using ModuleManager = System.Qizx.XQuery.ModuleManager;
using XQuerySessionImpl = System.Qizx.XQuery.XQuerySessionImpl;
namespace System.Qizx.Api {

    /// <summary> Manager of simple XQuery sessions without access to a XML Library.
    /// <para>
    /// Provides a cache of Modules and a cache of transient documents, shared among
    /// the sessions created on this manager. This cache avoids reparsing XML
    /// documents if different sessions access it. It can detect a change on
    /// documents stored in the file-system and reload the document.</para>
    /// </summary>
    public class XQuerySessionManager {
        /// <summary> For internal use.</summary>
        virtual public ModuleManager ModuleManager {
            get {
                return moduleMan;
            }

        }

        /// <summary> For internal use.</summary>
        public virtual DocumentPool DocumentCache {
            get {
                return documentCache;
            }
            set {
                this.documentCache = value;
            }

        }
        /// <summary> Returns the current Resolver of XQuery modules.</summary>
        /// <returns> the current module resolver
        /// </returns>
        /// <summary> Defines a resolver of XQuery modules.</summary>
        /// <param name="resolver">a module resolver
        /// </param>
        virtual public ModuleResolver ModuleResolver {
            get {
                return moduleMan.Resolver;
            }

            set {
                Check.nonNull(value, "resolver");
                moduleMan.Resolver = value;
            }

        }
        private ModuleManager moduleMan;
        private DocumentPool documentCache;

        /// <summary> Creates a session manager with a default Module Resolver and 
        /// a default cache for parsed documents.
        /// 
        /// </summary>
        /// <param name="moduleBaseURI">base URI for the default Module Resolver
        /// </param>
        public XQuerySessionManager(System.Uri moduleBaseURI)
            : this(new DefaultModuleResolver(moduleBaseURI), -1) {
        }

        /// <summary> Creates a session manager.</summary>
        /// <param name="moduleResolver">resolver used for modules
        /// </param>
        /// <param name="transientDocumentCacheSize">size in bytes of the document cache
        /// </param>
        public XQuerySessionManager(ModuleResolver moduleResolver, int transientDocumentCacheSize) {
            moduleMan = new ModuleManager(moduleResolver);
            documentCache = new DocumentPool();
            if (transientDocumentCacheSize >= 0)
                documentCache.setCacheSize(transientDocumentCacheSize);
        }

        /// <summary> Creates a new XQuery session.</summary>
        /// <returns> a new XQuery session using the resources of this session manager
        /// </returns>
        public virtual XQuerySession CreateSession() {
            return new XQuerySessionImpl(this);
        }

        /// <summary> Sets the maximum memory size for the document cache. The document cache
        /// stores transient documents which are parsed in memory.
        /// </summary>
        /// <param name="size">maximum memory size in bytes. Decreasing this size will
        /// flush the cache accordingly.
        /// </param>
        /// <returns> the former maximum memory size in bytes
        /// </returns>
        public virtual int SetTransientDocumentCacheSize(int size) {
            int oldSize = documentCache.getCacheSize();
            documentCache.setCacheSize(size);
            return oldSize;
        }

        /// <summary> Gets the current maximum memory size for the document cache.</summary>
        /// <returns> a size in bytes
        /// </returns>
        public virtual int GetTransientDocumentCacheSize() {
            return documentCache.getCacheSize();
        }
    }
}